<?php
declare(strict_types = 1);

namespace IMOcean\Server;

use Swoole\Http\Server;
use Swoole\Http\Request;
use Swoole\Http\Response;

class Http
{
    private string $host = '127.0.0.1';
    private int $port = 9503;

    private ?Server $server = null;

    public function __construct(string $host, int $port)
    {
        $this->host = $host;
        $this->port = $port;

        $this->initialize();
    }

    public function initialize()
    {
        $this->server = new Server($this->host, $this->port);

        $this->registerEvent();
    }

    public function registerEvent()
    {
        $this->on('Request', [$this, 'onRequest']);
    }

    public function run()
    {
        $this->server->start();
    }

    public function onRequest(Request $request, Response $response)
    {
        $response->header('Content-Type', 'text/html; charset=utf-8');
        
        $response->end('hello world!');
    }
}
